package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.customer.domain.FinanceAutobillException;
import com.ktgj.customer.domain.vo.BillDetailsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.FinanceAutobillRecord;
import com.ktgj.customer.service.IFinanceAutobillRecordService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 财务自动对账文件记录Controller
 * 
 * @author ruoyi
 * @date 2022-06-13
 */
@RestController
@RequestMapping("/autobillRecord")
public class FinanceAutobillRecordController extends BaseController
{
    @Autowired
    private IFinanceAutobillRecordService financeAutobillRecordService;

    /**
     * 查询财务自动对账文件记录列表
     */
    @PreAuthorize(hasPermi = "customer:record:list")
    @GetMapping("/list")
    public TableDataInfo list(FinanceAutobillRecord financeAutobillRecord)
    {
        startPage();
        List<FinanceAutobillRecord> list = financeAutobillRecordService.selectFinanceAutobillRecordList(financeAutobillRecord);
        return getDataTable(list);
    }

    /**
     * 导出财务自动对账文件记录列表
     */
    @PreAuthorize(hasPermi = "customer:record:export")
    @Log(title = "财务自动对账文件记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FinanceAutobillRecord financeAutobillRecord) throws IOException
    {
        List<FinanceAutobillRecord> list = financeAutobillRecordService.selectFinanceAutobillRecordList(financeAutobillRecord);
        ExcelUtil<FinanceAutobillRecord> util = new ExcelUtil<FinanceAutobillRecord>(FinanceAutobillRecord.class);
        util.exportExcel(response, list, "财务自动对账文件记录数据");
    }

    /**
     * 获取财务自动对账文件记录详细信息
     */
    @PreAuthorize(hasPermi = "customer:record:query")
    @GetMapping(value = "/{billRecordId}")
    public AjaxResult getInfo(@PathVariable("billRecordId") Long billRecordId)
    {
        return AjaxResult.success(financeAutobillRecordService.selectFinanceAutobillRecordById(billRecordId));
    }

    /**
     * 新增财务自动对账文件记录
     */
//    @PreAuthorize(hasPermi = "customer:record:add")
//    @Log(title = "财务自动对账文件记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(HttpServletRequest request, @RequestBody FinanceAutobillRecord financeAutobillRecord) {
        int i;
        try {
            i = financeAutobillRecordService.insertFinanceAutobillRecord(request, financeAutobillRecord);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (i > 0){
            return AjaxResult.success("添加成功");
        }
        else {
            return AjaxResult.success("添加失败");
        }
    }

    /**
     * 修改财务自动对账文件记录
     */
    @PreAuthorize(hasPermi = "customer:record:edit")
    @Log(title = "财务自动对账文件记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FinanceAutobillRecord financeAutobillRecord)
    {
        return toAjax(financeAutobillRecordService.updateFinanceAutobillRecord(financeAutobillRecord));
    }

    /**
     * 删除财务自动对账文件记录
     */
    @PreAuthorize(hasPermi = "customer:record:remove")
    @Log(title = "财务自动对账文件记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{billRecordIds}")
    public AjaxResult remove(@PathVariable Long[] billRecordIds)
    {
        return toAjax(financeAutobillRecordService.deleteFinanceAutobillRecordByIds(billRecordIds));
    }

    /**
     * 原始账单查询
     * */
    @PostMapping("/billingInquiry")
    public TableDataInfo billingInquiry(@RequestBody FinanceAutobillRecord financeAutobillRecord){
        startPage();
        List<FinanceAutobillRecord> financeAutobillRecordList = financeAutobillRecordService.billingInquiry(financeAutobillRecord);
        return getDataTable(financeAutobillRecordList);
    }

    /**
     * 原始账单详细
     * */
    @PostMapping("/billDetails")
    public AjaxResult billDetails(@RequestBody Map<String, Object> map){
        Long id = Long.valueOf(map.get("id").toString());
        List<BillDetailsVo> billDetailsVo;
        try {
            billDetailsVo = financeAutobillRecordService.billDetails(id);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(billDetailsVo);
    }

    /**
     * 对账
     */
    @PostMapping("/reconciliation")
    public AjaxResult reconciliation(HttpServletRequest request, @RequestBody FinanceAutobillRecord financeAutobillRecord){
        List<FinanceAutobillException> reconciliation;
        try {
            reconciliation = financeAutobillRecordService.reconciliation(request, financeAutobillRecord);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        return AjaxResult.success(reconciliation);
    }
}
